<template>
  <div>
    <def-layout type="col0-24" :page="page" @invoke="invoke">
      <div slot="search" class="form-items">
        <el-form-item>
          <form-remote-select
            v-model="formSearch.supplierId"
            size="mini"
            placeholder="供应商"
            :url="service.conf.api.get('wms').url+'/api/sup/supplier-info/map/0'"
          />
        </el-form-item>
        <el-form-item>
          <el-input v-model="formSearch.keyword" placeholder="采购单号" />
        </el-form-item>
        <el-form-item>
          <duty-users v-model="formSearch.salesmanId" placeholder="业务员" size="mini" mode="search" :duty-id="$store.state.settings.wmsConf.purchase.dutyId[1]" />
        </el-form-item>
        <el-form-item>
          <form-remote-select
            v-model="formSearch.destinationId"
            size="mini"
            placeholder="纸厂"
            :url="service.conf.api.get('wms').url+'/api/logistics/location/map'"
          />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="formSearch.date"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
      </div>
      <div slot="content" class="grid">
        <page-grid v-if="self!=null" ref="pageGrid" :parent="self">
          <div slot="columns">
            <el-table-column prop="createTime" sortable align="center" label="创建日期" width="150">
              <!-- <template slot-scope="scope">
                {{ scope.row.createTime.substring(0,10) }}
              </template> -->
            </el-table-column>
            <el-table-column prop="status" align="center" label="状态" width="100">
              <template slot-scope="scope">
                <column-filter v-model="scope.row.status" type="supOrderStatus" @click="filter" />
              </template>
            </el-table-column>
            <el-table-column prop="no" align="center" label="单号" width="180" />
            <el-table-column prop="license" align="center" label="车牌号" width="120" />
            <el-table-column prop="supplierName" align="center" label="供应商" width="150">
              <template slot-scope="scope">
                <column-filter-label v-model="scope.row.supplierId" :label="scope.row.supplierName" search-key="supplierId" @click="filter" />
              </template>
            </el-table-column>
            <el-table-column prop="salesmanName" align="center" label="业务员" width="150">
              <template slot-scope="scope">
                <column-filter-label v-model="scope.row.salesmanId" :label="scope.row.salesmanName" search-key="salesmanId" @click="filter" />
              </template>
            </el-table-column>
            <el-table-column prop="productNames" align="center" label="货品" width="150" />
            <el-table-column prop="settleQuantity" align="right" label="结算量(吨)" sortable width="120" />
            <el-table-column prop="deduct" align="right" label="扣点率(%)" sortable width="120" />
            <el-table-column prop="univalence" align="right" label="单价/吨" width="120" />
            <el-table-column prop="paymentAmount" align="right" label="结算金额" sortable width="150" />
          </div>
        </page-grid>
      </div>
    </def-layout>
    <pur-bacth-replying ref="replying" :parent="self" />
  </div>
</template>
<script>
import adminSupport from 'lola-admin-support-el'
import PurBacthReplying from '@/views/pur-group/check-manage/replying.vue'
export default {
  name: 'PurCheckManage',
  components: {
    PurBacthReplying
  },
  mixins: [adminSupport.mixins.index],
  data() {
    return {
      name: 'PurCheckManage',
      formSearch: { keyword: '', destinationId: '', supplierId: '', date: [] },
      self: null,
      paramsStatus: null // 路由参数
    }
  },
  mounted() {
    this.init()// 页面自定义初始化
  },
  methods: {
    init() {
      var that = this
      that.self = this
      // Excel导出配置
      this.page.excelParams = {
        fileds: ['createTime', 'license', 'supplierName', 'salesmanName', 'productNames', 'quantity', 'deduct', 'settleQuantity', 'univalence', 'actualAmount'],
        queryParam: this.dataGrid.params,
        size: 10000
      }
      that.dataGrid.params.queryList.push({ 'key': 'o.status', 'value': '10', 'oper': '=', 'join': 'and' })
      that.dataGrid.table.tool = false
      that.loadData()// 加载数据(按钮+列表) 扩展需重写该方法
    },
    search() {
      const queryList = []
      if (!String.isNullOrEmpty(this.formSearch.keyword)) queryList.push({ 'key': 'o.no', 'value': this.formSearch.keyword, 'oper': '=', 'join': 'and' })

      if (!String.isNullOrEmpty(this.formSearch.salesmanId)) queryList.push({ 'key': 'o.salesmanId', 'value': this.formSearch.salesmanId, 'oper': '=', 'join': 'and' })
      if (!String.isNullOrEmpty(this.formSearch.supOrderFlag)) queryList.push({ 'key': 'o.flag', 'value': this.formSearch.supOrderFlag, 'oper': '=', 'join': 'and' })
      if (!String.isNullOrEmpty(this.formSearch.license)) queryList.push({ 'key': 'o.license', 'value': this.formSearch.license, 'oper': '=', 'join': 'and' })
      if (!String.isNullOrEmpty(this.formSearch.startAreaIds)) {
        queryList.push({ 'key': 'sl.areaIds', 'value': this.formSearch.startAreaIds, 'oper': ':', 'join': 'and' })
      }
      if (!String.isNullOrEmpty(this.formSearch.destinationId)) queryList.push({ 'key': 'o.destinationId', 'value': this.formSearch.destinationId, 'oper': '=', 'join': 'and' })
      if (!String.isNullOrEmpty(this.formSearch.supplierId)) queryList.push({ 'key': 'o.supplierId', 'value': this.formSearch.supplierId, 'oper': '=', 'join': 'and' })

      if (!String.isNullOrEmpty(this.formSearch.endAreaIds)) {
        queryList.push({ 'key': 'el.areaIds', 'value': this.formSearch.endAreaIds, 'oper': ':', 'join': 'and' })
      }
      if (this.formSearch.date.length === 2) {
        queryList.push({ 'key': 'o.createTime', 'value': this.formSearch.date.join(','), 'oper': 'b', 'join': 'and' })
      }
      queryList.push({ 'key': 'o.status', 'value': '10', 'oper': '=', 'join': 'and' })
      this.dataGrid.params.queryList = queryList
      this.refresh()
    },
    bindSearch() {
      this.formSearch = { keyword: '', destinationId: '', salesmanId: '', supOrderFlag: '', supplierId: '', date: [] }
    },
    // 重写按钮方法
    buildOtherBtn(item, btn) {
      switch (item.code) {
        case 'm5281':
          btn.type = 'info'
          btn.opt = 'batchCheck'
          break
      }
    },
    batchCheck() {
      const that = this
      if (String.isNullOrEmpty(that.formSearch.supplierId)) {
        that.$message.error('请选择待审批供应商')
        return
      }
      if (that.page.ids.length === 0) {
        that.$message.error('请选择待审批采购单')
        return
      }
      // 审批总额
      let totalActualAmount = 0
      var actualAmounts = []
      that.$refs.pageGrid.getSelection().forEach(item => {
        totalActualAmount += item.paymentAmount
        actualAmounts.push(item.paymentAmount)
      })
      // console.log(orderPrice)
      that.$refs.replying.show(that.formSearch.supplierId, totalActualAmount, that.page.ids, actualAmounts)
    }
  }
}
</script>
